{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-02-22T07:47:54.687658Z",
     "start_time": "2024-02-22T07:47:54.683836Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['You can eat and eat, but nothing will ever fill the void. Welcome to the '\n",
      " 'real world! It sucks. You’re gonna love it!\\n',\n",
      " '\\n',\n",
      " 'I thought that it (propose) mattered what I said or where I said it. \\n',\n",
      " '\\n',\n",
      " 'Then I realized the only thing that matters is that you, you make me happier '\n",
      " 'than I ever thought I could be.\\n',\n",
      " '\\n',\n",
      " 'If I had known the last time I saw you would be the last time, I ... I would '\n",
      " 'have stopped to memorize your face, the way you move, everything about '\n",
      " 'you. \\n',\n",
      " '\\n',\n",
      " 'If I had known the last time I kissed you would have been the last time... I '\n",
      " 'never would have stopped.\\n',\n",
      " '\\n',\n",
      " 'Monica, I thought this (getting married) was going to be the most difficult '\n",
      " 'thing I ever gonna have to do. \\n',\n",
      " '\\n',\n",
      " 'But when I saw you walking down that aisle I realized how simple it was.\\n',\n",
      " '\\n',\n",
      " 'I love you. Any surprises that come our way it’s okay, because I will always '\n",
      " 'love you. You are the person I was meant to spend the rest of my life '\n",
      " 'with.\\n',\n",
      " '\\n',\n",
      " 'I say more sumb things before 9 am than most people say all day.\\n',\n",
      " '\\n',\n",
      " 'Ah, humor based on my pain.\\n',\n",
      " '\\n',\n",
      " 'No uterus, no opinion.\\n',\n",
      " '\\n',\n",
      " 'I have to go before I put your head through a wall.\\n',\n",
      " '\\n',\n",
      " 'You know, like a cow’s opinion. It doesn’t matter. It’s moo.\\n',\n",
      " '\\n',\n",
      " 'Your love is like a giant pigeon crapping on my heart. ']\n"
     ]
    }
   ],
   "source": [
    "# 采用pprint模块美化读取文件部分内容\n",
    "import pprint\n",
    "with open(\"./24-demo.txt\") as f:\n",
    "    file_data = f.readlines()\n",
    "    pprint.pprint(file_data)"
   ]
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "27\n"
     ]
    }
   ],
   "source": [
    "# 统计全部行数\n",
    "print(len(file_data))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T07:48:01.882161Z",
     "start_time": "2024-02-22T07:48:01.876061Z"
    }
   },
   "id": "b3fa12490c02119a",
   "execution_count": 6
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "13\n"
     ]
    }
   ],
   "source": [
    "# 统计空行行数\n",
    "print(file_data.count(\"\\n\"))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T07:48:04.048011Z",
     "start_time": "2024-02-22T07:48:04.042855Z"
    }
   },
   "id": "b3b8bfee506f9284",
   "execution_count": 7
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "14\n",
      "14\n"
     ]
    }
   ],
   "source": [
    "# 统计非空行行数\n",
    "print(len(file_data) - file_data.count(\"\\n\"))   # 人类固有思维完成\n",
    "print(len(set(file_data)) - 1)                  # 使用python的set的特点"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T07:54:07.015495Z",
     "start_time": "2024-02-22T07:54:07.010725Z"
    }
   },
   "id": "de737a146167ced9",
   "execution_count": 9
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "19\n"
     ]
    }
   ],
   "source": [
    "# 统计单词 I 出现的次数\n",
    "print(file_data.count(\"I\")) # 错误用法，结果为0\n",
    "# 将原来的数据类型转换为str，然后按空格拆分数据中的每个单词，最后再来统计I出现的次数\n",
    "print(str(file_data).split(\" \").count(\"I\"))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T07:58:13.955868Z",
     "start_time": "2024-02-22T07:58:13.952263Z"
    }
   },
   "id": "eebf96705b27a14d",
   "execution_count": 12
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "# 统计单词 you 和 You 出现的次数\n",
    "print(int(str(file_data).split(\" \").count(\"you\")))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T07:59:46.220455Z",
     "start_time": "2024-02-22T07:59:46.216517Z"
    }
   },
   "id": "ee0d6e6e6f6cf687",
   "execution_count": 14
  },
  {
   "cell_type": "code",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "print(int(str(file_data).split(\" \").count(\"You\")))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-02-22T08:00:15.051378Z",
     "start_time": "2024-02-22T08:00:15.044855Z"
    }
   },
   "id": "c0f1847fea621ba",
   "execution_count": 15
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
